﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.ESS.MODEL;
using KPIS.GERP.ESS.DAL;

namespace KPIS.GERP.ESS.BLL
{
	public class B_MessageSend
	{
		private ConvertNullable.ConvertNullable cn = new ConvertNullable.ConvertNullable();
		//private static readonly IEmployeeDAL empDAL = DataAccess.CreateEmployees();
		private static readonly D_MessageSend dal = new D_MessageSend();
		//D_FIM_MAS_Receives r = new D_FIM_MAS_Receives();
		//public DataSet LoadEmployees(Nullable<int> Sequence, string emp_type_seq, bool bActive)
		//{
		//    return empDAL.LoadEmployees(Sequence, emp_type_seq, bActive);
		//}
		public B_MessageSend()
		{

		}

		public List<M_MessageSend> GetFull(string send_seq)
		{
			Nullable<int> nsend_seq = cn.ConvertNullInt(send_seq);
			return dal.GetFull(null, null, null, null, null, null, null, nsend_seq);
		}

		public List<M_MessageSend> GetFull(string to_user_seq
			, string from_user_seq, string message_status, string job_site_seq, string ref_type, string ref_seq
			, List<MpPositionWorkOnDutyVM> wods = null, string send_seq = null)
		{
			Nullable<int> nto_user_seq = cn.ConvertNullInt(to_user_seq);
			Nullable<int> nfrom_user_seq = cn.ConvertNullInt(from_user_seq);
			Nullable<int> njob_site_seq = cn.ConvertNullInt(job_site_seq);
			Nullable<int> nref_seq = cn.ConvertNullInt(ref_seq);
			Nullable<int> nsend_seq = cn.ConvertNullInt(send_seq);
			return dal.GetFull(nto_user_seq, nfrom_user_seq, message_status, njob_site_seq, ref_type, nref_seq, wods,
				nsend_seq);
		}

		public List<M_MessageSend> GetFullWithApproveCommandStatus(string to_user_seq
			, string from_user_seq, string message_status, string job_site_seq, string ref_type, string ref_seq
			, List<MpPositionWorkOnDutyVM> wods = null, string send_seq = null)
		{
			Nullable<int> nto_user_seq = cn.ConvertNullInt(to_user_seq);
			Nullable<int> nfrom_user_seq = cn.ConvertNullInt(from_user_seq);
			Nullable<int> njob_site_seq = cn.ConvertNullInt(job_site_seq);
			Nullable<int> nref_seq = cn.ConvertNullInt(ref_seq);
			Nullable<int> nsend_seq = cn.ConvertNullInt(send_seq);
			return dal.GetFullWithApproveCommandStatus(nto_user_seq, nfrom_user_seq, message_status, njob_site_seq,
				ref_type, nref_seq, wods, nsend_seq);
		}

		public List<M_MessageSend> GetApprovePath(string ref_type, string ref_seq)
		{
			Nullable<int> nref_seq = cn.ConvertNullInt(ref_seq);
			return dal.GetApprovePath(ref_type, nref_seq);
		}

		public List<M_MessageSend> GetList(string to_user_seq, string from_user_seq, string message_status)
		{
			Nullable<int> nto_user_seq = cn.ConvertNullInt(to_user_seq);
			Nullable<int> nfrom_user_seq = cn.ConvertNullInt(from_user_seq);
			return dal.GetList(nto_user_seq, nfrom_user_seq, message_status);
		}

		public Nullable<int> GetPosSeqFromEmpSeq(string emp_seq)
		{
			Nullable<int> nemp_seq = cn.ConvertNullInt(emp_seq);
			return dal.GetPosSeqFromEmpSeq(nemp_seq);
		}

		public void WriteLog(string stringMessage)
		{
			dal.WriteLog(stringMessage);
		}

		public DataTable GetCurrentApprover(string str_emp_seq, string str_send_seq, string process, int step_no)
		{
			Exception excParamWrong = new Exception("Need param either emp_seq or send_seq");
			Exception excMessageSendNotFound = new Exception("Not found message_send = " + str_send_seq);
			Exception excToPosSeqValue = new Exception("Null to_pos_seq where send_seq = " + str_send_seq);

			Nullable<int> n_emp_seq = cn.ConvertNullInt(str_emp_seq);
			Nullable<int> n_pos_seq = dal.GetPosSeqFromEmpSeq(n_emp_seq);
			Nullable<int> n_send_seq = cn.ConvertNullInt(str_send_seq);

			M_MessageSend message_send = GetFull(str_send_seq).FirstOrDefault();

            // new requirement
			// validate param step 1
            //if ((!n_pos_seq.HasValue && !n_send_seq.HasValue) || (n_pos_seq.HasValue && n_send_seq.HasValue))
            //    throw excParamWrong;

			// validate param step 2
			if (n_send_seq.HasValue && message_send == null) throw excMessageSendNotFound;
            if (n_send_seq.HasValue && !message_send.to_pos_seq.HasValue && message_send.pos_grp_seq == null) throw excToPosSeqValue;

			// determine pos_seq by param emp_seq || approver_send_seq
            if (message_send.to_pos_seq.HasValue)
            {
                n_pos_seq = n_send_seq.HasValue ? message_send.to_pos_seq.Value : n_pos_seq.Value;
            }
            else
            {
                //n_pos_seq = n_pos_seq;
            }

			// retreive approver data from pos_seq
			return dal.GetCurrentApprover(n_pos_seq, process, step_no);
		}

		public string GetNextStepNo(string approve_path_seq, string amount)
		{
			Nullable<int> napprove_path_seq = cn.ConvertNullInt(approve_path_seq);
			double? dAmount = cn.ConvertNullDouble(amount);
			return dal.GetNextStepNo(napprove_path_seq, dAmount);
		}

		public string GetLastStepNo(string process, double? Amount = 0)
		{
			return dal.GetLastStepNo(process, Amount);
		}

		public string SelectRecentInsertedRoleSeq()
		{
			return dal.SelectRecentInsertedRoleSeq();
		}

		public M_MessageSend GetInfo(string seq)
		{
			int nSeq;

			if (seq == null)
				return null;

			if (cn.IsNumeric(seq))
			{
				nSeq = int.Parse(seq);
				return dal.GetInfo(nSeq);
			}
			else
			{
				return null;
			}
		}

		public M_MessageSend GetInfoFromRefSeq(string ref_seq, string ref_type)
		{
			int nSeq;

			if (ref_seq == null)
				return null;

			if (cn.IsNumeric(ref_seq))
			{
				nSeq = int.Parse(ref_seq);
				return dal.GetInfoFromRefSeq(nSeq, ref_type);
			}
			else
			{
				return null;
			}
		}

		public int GetUnreadCount(string emp_seq)
		{
			int nSeq;

			if (emp_seq == null)
				return 0;

			if (cn.IsNumeric(emp_seq))
			{
				nSeq = int.Parse(emp_seq);
				return dal.GetUnreadCount(nSeq);
			}
			else
			{
				return 0;
			}
		}

		public int GetSendUnwarnUnreadCount(int emp_seq, out int nUnwarnCount, out int nUnreadCount)
		{
			return dal.GetSendUnwarnUnreadCount(emp_seq, out nUnwarnCount, out nUnreadCount);
		}

		public M_MessageSend GetInfoFromFromSendSeq(string seq)
		{
			int nSeq;

			if (seq == null)
				return null;

			if (cn.IsNumeric(seq))
			{
				nSeq = int.Parse(seq);
				return dal.GetInfoFromFromSendSeq(nSeq);
			}
			else
			{
				return null;
			}
		}

		public M_MessageSend GetLatestMessageStatus(string ref_seq, string ref_type)
		{
			Nullable<int> nref_seq = cn.ConvertNullInt(ref_seq);
			return dal.GetLatestMessageStatus(nref_seq, ref_type);
		}

		public void Insert(M_MessageSend info)
		{
			try
			{
				dal.Insert(info);
			}
			catch
			{
				throw;
			}
		}

		public void Update(M_MessageSend info)
		{
			try
			{
				dal.Update(info);
			}
			catch
			{
				throw;
			}
		}

		public void Delete(M_MessageSend info)
		{
			try
			{
				dal.Delete(info);
			}
			catch
			{
				throw;
			}
		}

		public string GetRefNoOfSendSeq(int? sendSeq)
		{
			return dal.GetRefNoOfSendSeq(sendSeq);
		}

		public void UpdateMessageStatus(M_MessageSend info)
		{
			try
			{
				dal.UpdateMessageStatus(info);
			}
			catch
			{
				throw;
			}
		}

		public string GetNextSendRefNo(string RequestType)
		{
			return dal.GetNextSendRefNo(RequestType);
		}

        public DataTable GetEmployeeInPositionGroup(string PosGrpSeq)
        {
            return dal.GetEmployeeInPositionGroup(PosGrpSeq);
        }
	}
}